<?php require(dirname(__FILE__)."/../../globals.inc"); $mikron->Page->StartBody(); ?>
<?php

    function smsunitaddlog($message)
    {
        $log = new T_LOGRECORD();
        $log->category->value = 1;
        $log->message->value = $message;
        $log->Save();       
    }

    $files = Array();
    $files[] = Array('marketitem'=>"silver", 'file'=>"http://smspartner.ru/export/numbers.php?partner=74681");
    $files[] = Array('marketitem'=>"gold", 'file'=>"http://smspartner.ru/export/numbers.php?partner=74761");
    $files[] = Array('marketitem'=>"platinum", 'file'=>"http://smspartner.ru/export/numbers.php?partner=74781");

    $rowattr = Array("operator", "snumber", "price", "prefix");
    $xmldata = Array();

    foreach($files as $filedata)
    {
        $marketitem = $filedata['marketitem'];
        $file = $filedata['file'];
        $dom = new DOMDocument();
        $dom->load($file);
        $datatag = $dom->getElementsByTagName('data');
        foreach($datatag as $data)
        {
            $rows = $data->getElementsByTagName("row");
            foreach($rows as $row)
            {
                $rowdata = Array();
                $rowdata["marketitem"] = $marketitem;
                reset($rowattr);
                foreach($rowattr as $ra)
                {
                    if($row->hasAttribute($ra))
                    {
                        $rowdata[$ra] = $row->getAttribute($ra);
                    }
                }
                $xmldata[] = $rowdata;
            }
        }
    }

    foreach($xmldata as $x)
    {
        $operator = $x['operator'];
        $op = new T_PHONEOPERATOR();
        $op->title->value = $operator;
        try
        {
            $op->Save();
            smsunitaddlog("Добавлен GSM оператор $operator.");
            echo "Оператор $operator добавлен.<br />";
        }
        catch(Exception $ex)
        {
            echo "<font color=red>Оператор $operator существует.</font><br />";
        }
    }
    
    reset($xmldata);
    foreach($xmldata as $x)
    {
        $operator = $x['operator'];
        $snumber = $x['snumber'];
        $price = Ceil($x['price'] * 100);
        $prefix = $x['prefix']."+{{cabinet}}";
        $marketitem = $x['marketitem'];

        $snumber2 = quote_smart($snumber);
        $prefix2 = quote_smart($prefix);
        $price2 = $price;
        $marketitem2 = quote_smart($marketitem);        
        $operator2 = quote_smart($operator);
        
        // echo "$price &rarr; $price2<br />";

        $market_items = $mikron->Queries->Query("MARKET_ITEM", null, new Criterion('code', $marketitem));
        if($market_items->count() < 1)
        {
            echo "<font color=red>Не найден товар $marketitem.</font><br />";
        }
        else
        {
            $market_item = $market_items->fetch();
            $op = $mikron->Queries->Query('T_PHONEOPERATOR', null, "`title`=$operator2");
            if($op->count() < 1)
            {
                echo "<font color=red>не найден оператор $operator.</font><br />";
            }
            else
            {
                $op = $op->fetch();
                $opid = $op->id;
                $mrkitmid = $market_item->id;

                $cr = new Criteria();
                $cr->Add(new Criterion('item', $mrkitmid));
                $cr->Add(new Criterion('phoneoperator', $opid));
                $cr->Add(new Criterion('smsnumber', $snumber2));
                $cr->Add(new Criterion('smscode', $prefix2));
                $cr->Add(new Criterion('price', $price2));

                $units = $mikron->Queries->Query('SMS_BILLING_UNIT', null, $cr);
                if($units->count() > 0)
                {
                    echo '<font color=red>SMS-купон уже существует.</font><br />';
                }
                else
                {
                    try
                    {
                        $unit = new SMS_BILLING_UNIT();
                        $unit->item->value = $mrkitmid;
                        $unit->phoneoperator->value = $opid;
                        $unit->smsnumber->value = $snumber;
                        $unit->smscode->value = $prefix;
                        $unit->price->value = $price;
                        $unit->Save();
                        echo 'SMS-купон добавлен.<br />';
                    }
                    catch(Exception $ex)
                    {
                        // CriticalError($ex->getMessage(), null, true);
                        echo '<font color=red>SMS-купон, ошибка: '.$ex->getMessage().".</font><br />";
                    }
                }
            }
        }
    }

?>
<?php $mikron->Page->EndBody(); ?>